﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Linq;

namespace Flame.Dlr
{
    public static class ManagerHelper
    {
        public static Result Execute(this IExecEngines exec, Type type, string text)
        {
            return exec.GetEngine(type).Execute(text);
        }

        public static ManagerSettings GetFromAllExecutableInCurrentDomain()
        {
            ManagerSettings settings = new ManagerSettings();
            var x = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (a.FullName.Contains("Flame.Dlr"))
                {
                    foreach (Type t in a.GetTypes())
                    {
                        if (t != typeof(NoneExec) && t.IsClass && !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null)
                        {
                            try
                            {

                                if (t.GetInterfaces().Contains(typeof(ICompilable)))
                                {
                                    //ICompilable c= (ICompilable)Activator.CreateInstance(t);
                                    //execsType.Add(t);
                                    settings.CompilersType.Add(t);
                                }
                                else if (t.GetInterfaces().Contains(typeof(IExecutable)))
                                {
                                    settings.ExecsType.Add(t);//(IExecutable)Activator.CreateInstance(t));
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
            return settings;
        }
    }
}
